package com.wzx.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;
import com.wzx.basics.utils.PageInfo;
import com.wzx.basics.utils.PageUtil;
import com.wzx.basics.utils.ResultUtil;
import com.wzx.data.entity.Demo;
import com.wzx.data.entity.StudentWork;
import com.wzx.data.entity.Subject;
import com.wzx.data.utils.NullUtils;
import com.wzx.data.entity.Teacher;
import com.wzx.data.service.ITeacherService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wzx.data.vo.SubjectVo;
import com.wzx.data.vo.TeacherVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 教师 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "教师管理接口")
@RequestMapping("/wzx/teacher")
@Transactional
public class TeacherController {

    @Autowired
    private ITeacherService iTeacherService;

    @ApiOperation("新增")
    @PostMapping("/save")
    public Result save(TeacherVo vo) {
        System.out.println("Vo:"+vo);
        return iTeacherService.saveTeacher(vo);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑")
    public Result update(Teacher teacher) {
        iTeacherService.updateById(teacher);
        return ResultUtil.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam String[] ids) {
        for(String id : ids){
            iTeacherService.deleteTeacher(id);
        }
        return ResultUtil.success();
    }
    @ApiOperation("查询讲师列表")
    @GetMapping("/list")
    public Result list(){
        List<Teacher> list = iTeacherService.list();
        return ResultUtil.data(list);
    }
    @ApiOperation("根据Id获取")
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    public Result<Teacher> getOne(@RequestParam String id){
        return new ResultUtil<Teacher>().setData(iTeacherService.getById(id));
    }
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iTeacherService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部数据")
    public Result<List<Teacher>> getAll(){
        return new ResultUtil<List<Teacher>>().setData(iTeacherService.list());
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result page(@ModelAttribute TeacherVo vo,@ModelAttribute PageVo pageVo){
        System.out.println(vo);
        final LambdaQueryWrapper<Teacher> lambda = new QueryWrapper<Teacher>().lambda();
        this.buildCondition(lambda,vo);
        final IPage<Teacher> page = iTeacherService.page(new Page<>(pageVo.getPageNumber(), pageVo.getPageSize()), lambda);
        PageInfo<Teacher> pageInfo = new PageInfo<>(pageVo.getPageNumber(), pageVo.getPageSize());
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setTotalList(page.getRecords());
        return ResultUtil.data(pageInfo);
    }
    /**
     * 构建查询条件
     * @param lambda
     * @param vo
     */
    private void buildCondition(LambdaQueryWrapper<Teacher> lambda, TeacherVo vo) {
        if(vo==null){
            return;
        }
        //根据教师姓名
        if (ObjectUtils.isNotEmpty(vo.getName())){
            lambda.like(Teacher::getName, vo.getName());
        }
        //根据级别
        if (ObjectUtils.isNotEmpty(vo.getLevel())){
            lambda.like(Teacher::getLevel, vo.getLevel());
        }
        //根据startTime
        if (ObjectUtils.isNotEmpty(vo.getStartTime())){
            lambda.ge(Teacher::getCreateTime, vo.getStartTime());
        }
        //根据endTime
        if (ObjectUtils.isNotEmpty(vo.getEndTime())){
            lambda.le(Teacher::getCreateTime, vo.getEndTime());
        }
        lambda.eq(Teacher::getDelFlag,0);
        lambda.orderByDesc(Teacher::getCreateTime);
    }
}
